#include <gnome.h>
#include <stdbool.h>
#include <sys/time.h>

static GArray *g_btns = NULL;
static GtkWidget *g_lab_sec = NULL;
static GtkWidget *g_lab_best_sec = NULL;
static int g_p_idx = -1;
static guint g_timer = -1;

static bool is_cheat = false;
//static bool is_round_over = true;

static double g_sec = 0;
static double g_best_sec = 999.0;

static struct timeval g_tv_start;


//以当前时间填充timeval结构
void set_timeval_now(struct timeval *ptv){
	g_return_if_fail(ptv);
	gettimeofday(ptv,NULL);
}

double calc_passed_sec(void){
	struct timeval tv_end;
	set_timeval_now(&tv_end);
	double sec = ((tv_end.tv_sec - g_tv_start.tv_sec)*1000000 + tv_end.tv_usec - \
		g_tv_start.tv_usec)/1000000.0;
	return sec;
}

void flash_lab_sec(void){
	g_sec = calc_passed_sec();
	gtk_label_set_label(GTK_LABEL(g_lab_sec),g_strdup_printf("%0.4f sec",g_sec));
}

void flash_lab_best_sec(void){
	gtk_label_set_label(GTK_LABEL(g_lab_best_sec),g_strdup_printf("最好记录 %0.4f sec",g_best_sec));
}

void update_best_sec(void){
	if(g_sec < g_best_sec){
		g_best_sec = g_sec;
		flash_lab_best_sec();
	}
}


void unmark_p_button(void){
	if(g_p_idx == -1) return;
	GtkWidget *p_btn = g_array_index(g_btns,GtkWidget*,g_p_idx);
	gtk_widget_modify_bg(p_btn,GTK_STATE_NORMAL,NULL);
}

void mark_p_button(void){
	if(g_p_idx == -1) return;
	GdkColor color;
	if(!gdk_color_parse("red",&color)){
		g_print("pase color failed!!!\n");
		color.red = 0x5555;
		color.green = 0x4444;
		color.blue = 0x3333;
	}
	g_print("color is %s\n",gdk_color_to_string(&color));
	GtkWidget *p_btn = g_array_index(g_btns,GtkWidget*,g_p_idx);
	gtk_widget_modify_bg(p_btn,GTK_STATE_NORMAL,&color);
}

void put_g(void){
	if(g_p_idx == -1) return;
	GtkWidget *p_btn = g_array_index(g_btns,GtkWidget*,g_p_idx);
	gtk_button_set_label(GTK_BUTTON(p_btn),"g");
	unmark_p_button();
}

void put_p(void){
	//还原p按钮原来的状态
	put_g();
	g_p_idx = random() % g_btns->len;
	GtkWidget *p_btn = g_array_index(g_btns,GtkWidget*,g_p_idx);
	gtk_button_set_label(GTK_BUTTON(p_btn),"p");

	if(is_cheat){
		mark_p_button();
	}
}


gboolean cb_timeout(gpointer data){
	flash_lab_sec();
	if(g_p_idx == -1){
		g_timer = -1;
		update_best_sec();
		return G_SOURCE_REMOVE;
	}
	else
		return G_SOURCE_CONTINUE;

}

void cb_start_find(GtkWidget *self,gpointer data){
	put_p();

	set_timeval_now(&g_tv_start);
	if(g_timer == -1){
		g_timer = g_timeout_add(50,cb_timeout,NULL);
	}

}

void cb_cheat_toggled(GtkWidget *self,gpointer data){
	is_cheat = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self));
	if(is_cheat)
		mark_p_button();
	else
		unmark_p_button();
}

void cb_clicked(GtkWidget *self,gpointer data){
	GtkWindow *w = GTK_WINDOW(data);
	GtkButton *btn = GTK_BUTTON(self);
	const gchar *str = gtk_button_get_label(btn);
	if(strcmp(str,"p") == 0){
		put_g();
		g_p_idx = -1;

		g_print("Bingoooo!you find p!\n");
		GtkWidget *dialog = gnome_ok_dialog_parented("Bigooooo!!! You find p!",GTK_WINDOW(w));
		gtk_window_set_modal(GTK_WINDOW(dialog),TRUE);
	}else
		g_print("wrong choose!!!!\n");
}

void add_buttons(GtkWidget *layout,GtkWidget *w){
	GtkWidget *button;

	g_btns = g_array_new(FALSE,FALSE,sizeof(GtkWidget*));

	for(int i=10;i<1000;i+=50){
		for(int j=10;j<1000;j+=50){
			//button = gtk_button_new_with_label(g_strdup_printf("(%d,%d)",i,j));
			button = gtk_button_new_with_label("g");
			g_array_append_val(g_btns,button);
			gtk_layout_put(GTK_LAYOUT(layout),button,i,j);
			gtk_signal_connect(GTK_OBJECT(button),"clicked",G_CALLBACK(cb_clicked),w);
		}
	}
	g_print("%s:ary count is %d\n",__func__,g_btns->len);
	//g_p_idx = random() % g_btns->len;
	//GtkWidget *p_btn = g_array_index(g_btns,GtkWidget*,g_p_idx);
	//gtk_button_set_label(GTK_BUTTON(p_btn),"p");
}

GtkWidget *make_layout(GtkWidget *w){
	GtkWidget *layout; 	//,*vscroll,*hscroll,*table;
	GtkAdjustment *hadj,*vadj;

	vadj = (GtkAdjustment*)gtk_adjustment_new(0.0,0.0,1050.0,1.0,10.0,20.0);
	hadj = (GtkAdjustment*)gtk_adjustment_new(0.0,0.0,1050.0,1.0,10.0,20.0);

	GtkWidget *scrolled_w = gtk_scrolled_window_new(hadj,vadj);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_w),GTK_POLICY_AUTOMATIC,
		GTK_POLICY_ALWAYS);


	layout = gtk_layout_new(hadj,vadj);
	gtk_layout_freeze(GTK_LAYOUT(layout));
	//设置layout整体大小
	gtk_layout_set_size(GTK_LAYOUT(layout),1050,1050);
	//设置layout可见部分的大小
	gtk_widget_set_size_request(GTK_WIDGET(layout),600,600);
	add_buttons(layout,w);
	gtk_layout_thaw(GTK_LAYOUT(layout));

	/*
	vscroll = gtk_vscrollbar_new(vadj);
	hscroll = gtk_hscrollbar_new(hadj);
	table = gtk_table_new(2,2,FALSE);
	gtk_table_attach_defaults(GTK_TABLE(table),layout,0,1,0,1);
	gtk_table_attach(GTK_TABLE(table),vscroll,1,2,0,1,GTK_FILL,GTK_FILL,0,0);
	gtk_table_attach(GTK_TABLE(table),hscroll,0,1,1,2,GTK_FILL,GTK_FILL,0,0);
	*/

	gtk_container_add(GTK_CONTAINER(scrolled_w),layout);
	//return table;
	return scrolled_w;

}

int main(int argc,char **argv){
	GtkWidget *w,*layout;

	gnome_init("","",argc,argv);
	w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_position(GTK_WINDOW(w),GTK_WIN_POS_CENTER);
	//gtk_window_set_default_size(GTK_WINDOW(w),200,200);
	gtk_window_set_title(GTK_WINDOW(w),"大家来找p :)");
	gtk_container_set_border_width(GTK_CONTAINER(w),20);
	gtk_signal_connect(GTK_OBJECT(w),"destroy",G_CALLBACK(gtk_main_quit),NULL);

	GtkWidget *vbox = gtk_vbox_new(FALSE,5);
	gtk_container_add(GTK_CONTAINER(w),vbox);
	GtkWidget *hbox = gtk_hbox_new(TRUE,5);
	gtk_container_add(GTK_CONTAINER(vbox),hbox);
	g_lab_sec = gtk_label_new("0 sec");
	gtk_widget_set_size_request(GTK_WIDGET(g_lab_sec),100,25);
	gtk_container_add(GTK_CONTAINER(hbox),g_lab_sec);
	GtkWidget *chk_cheat = gtk_check_button_new_with_label("开启作弊");
	gtk_container_add(GTK_CONTAINER(hbox),chk_cheat);
	gtk_signal_connect(GTK_OBJECT(chk_cheat),"toggled",G_CALLBACK(cb_cheat_toggled),NULL);

	GtkWidget *btn = gtk_button_new_with_label("开始找p!");
	gtk_container_add(GTK_CONTAINER(hbox),btn);
	gtk_signal_connect(GTK_OBJECT(btn),"clicked",G_CALLBACK(cb_start_find),NULL);

	g_lab_best_sec = gtk_label_new("最好记录 -1 sec");
	gtk_widget_set_size_request(GTK_WIDGET(g_lab_best_sec),100,25);
	gtk_container_add(GTK_CONTAINER(hbox),g_lab_best_sec);


	layout = make_layout(w);
	gtk_container_add(GTK_CONTAINER(vbox),layout);
	gtk_widget_show_all(w);
	gtk_main();

	return 0;
}